import Vue from 'vue';
import axios from 'axios';
// import router from '@/router/index';
import conf from '@/config';

// import { errorCode } from '@/utils/enum';

// 创建 axios 实例
const http = axios.create({
  baseURL: conf.baseURL,
  timeout: 0,
});

// 发送请求拦截处理
http.interceptors.request.use(
  (request) => {
    const newRequest = request;
    newRequest.params = {
      _t: new Date().getTime(),
      ...newRequest.params,
    };
    const token = Vue.prototype.$getLS('accessToken');
    newRequest.headers.Authorization = `Bearer ${token}`;
    return newRequest;
  },
  (error) => {
    const er = error;
    return Promise.reject(er);
    // console.log(error);
    // for (const [k, v] of Object.entries(error)) {
    //   console.log(k, v);
    // }
  },
);

// 返回拦截处理
http.interceptors.response.use(
  response => response,
  // response => {
  //   // console.log('============');
  //   // console.log(response);
  //   return response;
  // },
  (error) => {
    for (const [k, v] of Object.entries(error)) {
      console.log(k, v);
    }
    if (error.response) {
      Vue.prototype.$message.error(error.response.data.message);
      // if (error.response.data.errorCode) {
      //   Vue.prototype.$message.error(errorCode[error.response.data.errorCode]);
      // } else {
      //   Vue.prototype.$message.error(error.response.data.message);
      // }
    } else {
      Vue.prototype.$message.error('权限异常，请重新登录！');
      Vue.prototype.$removeLS('accessToken');
    }
    // else if (typeof (error.response) === 'undefined') {
    //   Vue.prototype.$message.error('响应超时,请重试');
    // }
  },
);

export default http;

// 未改变上传文件的文件名
export const requestFile = (file, cb) => {
  http
    .get('/edges/oss/qiniu/token/uploading')
    .then((res) => {
      if (!res) {
        return;
      }
      const uptoken = res.data.uptoken;
      const formData = new FormData();
      formData.append('file', file);
      formData.append('token', uptoken);
      http
      .post('http://up-z1.qiniu.com/', formData)
      .then((re) => {
        if (!re) {
          return;
        }
        cb(re);
      });
    });
};

// 未保留上传文件的文件名
export const requestFileName = (file, cb) => {
  http
    .get('/edges/oss/qiniu/token/uploading?keepSrcFileName=false')
    .then((res) => {
      if (!res) {
        return;
      }
      const uptoken = res.data.uptoken;
      const formData = new FormData();
      formData.append('file', file);
      formData.append('token', uptoken);
      http
      .post('http://up-z1.qiniu.com/', formData)
      .then((re) => {
        if (!re) {
          return;
        }
        cb(re);
      });
    });
};

export const requestFileHead = (file, cb) => {
  http
    .get('/edges/oss/qiniu/token/uploading')
    .then((res) => {
      if (!res) {
        return;
      }
      const uptoken = res.data.uptoken;
      const formData = new FormData();
      formData.append('file', file, 'touxiang.jpg');
      formData.append('token', uptoken);
      http
      .post('http://up-z1.qiniu.com/', formData)
      .then((re) => {
        if (!re) {
          return;
        }
        cb(re);
      });
    });
};
